<?php

namespace app\admin\model;

class Studentmodel extends Commonmodel {

    protected $croomTable = 'zhjs_classroom';
    protected $signTable = 'zhjs_student_sign';
    protected $creTable = 'zhjs_courseware_record';

    public function __construct(array $data = [])
    {
        $this->table = 'zhjs_student';
        $this->pk = 'id';
        parent::__construct($data);
    }

    public function getAllStudentSign($where = null, $pageParam = null, $whereExp = null, $num = 20){
        $this->table = 'zhjs_student';
        $whereData = [['s.is_delete' , '=', 1], ['sg.is_delete' , '=', 1]];
        $paginate = ['list_rows'=>$num];

        if($where)  $whereData = array_merge($where, $whereData);
        if(!$whereExp)  $whereExp = '1=1';
        if($pageParam = array_splice($pageParam, 1))  $paginate = array_merge($paginate, ['query'=>$pageParam]);

        $select = 'sg.id, cr.name, s.username, s.nickname, sg.create_date, c.title';

        $data = self::db()
            ->alias('s')
            ->leftJoin([$this->signTable=>'sg'],'s.id=sg.sid')
            ->leftJoin([$this->croomTable=>'cr'],'sg.crid=cr.id')
            ->leftJoin([$this->creTable=>'c'],'sg.cid=c.id')
            ->field($select)
            ->where($whereData)
            ->whereExp('', $whereExp)
            ->order(['sg.id'=>'desc'])
            ->paginate($paginate);

        if(!$data)  return false;

        $list['page'] = $data->render();
        $dataArr = $data->toArray();
        $list['data'] = $dataArr['data'];

        return $list;
    }

    public function getDetailStudentSignById($id){
        $this->table = 'zhjs_student';
        if(!$id)  return false;

        $select = 'sg.id, cr.name, s.username, s.nickname, sg.create_date, c.title';

        $data = self::db()
            ->alias('s')
            ->leftJoin([$this->signTable=>'sg'],'s.id=sg.sid')
            ->leftJoin([$this->croomTable=>'cr'],'sg.crid=cr.id')
            ->leftJoin([$this->creTable=>'c'],'sg.cid=c.id')
            ->field($select)
            ->where([['sg.id', '=', $id]])
            ->find()->toArray();

        if(!$data)  return false;
        return $data;
    }

}

?>